<?php
namespace Admin\Controller;
use Tool\AdminController;

class TestController extends AdminController {
	/**
	 * 试题录入
	 */
	function entering(){
		$type = D('Type');
		$en_name = $type->where('value=0')->select();
		$this->assign('en_name',$en_name);
		$question = new \Model\QuestionModel();
		if (!empty($_POST)) {
			if ($_POST['content1'] != '<strong>请输入试题内容</strong>' && $_POST['answer1'] != false) {
				unset($_POST['content']);
				unset($_POST['answer']);
				$_POST['content'] = $_POST['content1'];
				$_POST['answer'] = $_POST['answer1'];
			}
			
			if ($_POST['content2'] != '<strong>请输入试题内容</strong>' && $_POST['answer2'] != false) {
				unset($_POST['content']);
				unset($_POST['answer']);
				$_POST['content'] = $_POST['content2'];
				$_POST['answer'] = $_POST['answer2'];
			}
			
			if (!empty($_POST['content'])) {
				$dirname = 'Upload/question/';
				$_POST['content'] = '{literal}' . $_POST['content'] . '{/literal}';
				$z = $question->writefile($dirname, $_POST['content']);
				if ($z) {
					$_POST['content'] = $z;
				}
			}
			
			if (!empty($_POST['answer'])) {
				$dirname = 'Upload/answer/';
				$_POST['answer'] = '{literal}' . $_POST['answer'] . '{/literal}';
				$z = $question->writefile($dirname, $_POST['answer']);
				if ($z) {
					$_POST['answer'] = $z;
				}
			}
			$_POST['add_time'] = strtotime(date('Y-m-d H:i:s'));
			unset($_POST['content1']);unset($_POST['content2']);unset($_POST['answer1']);unset($_POST['answer2']);
		}
		$z = $question->add($_POST);
		if ($z) {
			$this->redirect('Tips/tips');
		}
		
		include ('top.php');
		$this->display();
	}
	/**
	 * 试题查看
	 */
	function see() {
		$type = D('Type');
		$qu = new \Model\QuestionModel();
		$info = $type->select();
		$this->assign('info',$info);

		include ('top.php');
		$this->display();
	}
	/**
	 * 编辑分类
	 */
	function type() {
		$type = D('Type');
		$qu = new \Model\QuestionModel();
		$info = $type->where('type_id=' . $_GET['type_id'])->find();
		$info['info'] = $qu->readfile($info['info']);
		$this->assign('info',$info);
		
		if (!empty($_POST)) {
			if (!empty($_POST['info'])) {
				$z = $qu->writefile('Upload/info/', $_POST['info']);
				$_POST['info'] = $z;
				unlink($info['info']);
			}
			$zz = $type->where('type_id=' . $_GET['type_id'])->save($_POST);
			if ($zz) {
				$this->redirect('Tips/tips');
			}
		}
		
		include ('top.php');
		$this->display();
	}
	/**
	 * 试题列表
	 */
	function exam() {
		$type = D('Type');
		$name = $type->where('value='. $_GET['type'])->find();
		$this->assign('name',$name);
		$question = new \Model\QuestionModel();
		$total = $question->where('type=' . $_GET['type'])->count();
		$page = new \Think\Page($total,10);
		$page->setConfig('header', '条记录');
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$page->setConfig('first', '第一页');
		$page->setConfig('end', '最末页');
		$page->setConfig('theme', '总共%TOTAL_ROW% %HEADER%，共%TOTAL_PAGE%页，当前第%NOW_PAGE%页|%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% ');
		$show = $page->show();
		$this->assign('page',$show);
		$sql = 'select ts_type.value,ts_type.type_name,ts_question.* from ts_question inner join ts_type on ts_question.type=ts_type.value where ts_question.type=' 
				. $_GET['type'] . ' order by id limit ' . $page->firstRow . ',' . $page->listRows;
		$qu = $question->query($sql);
		$this->assign('qu',$qu);
		
		//批量删除
		if (!empty($_POST['checkbox'])) {
			$check = implode(',',$_POST['checkbox']);
			$z = $question->batch($check);
			if ($z) {
				$this->redirect('Tips/tips');
			}
		}

		include ('top.php');
		$this->display();
	}
	/**
	 * 修改试题
	 */
	function upd() {
		$que = new \Model\QuestionModel();
		$upd = $que->where('id=' . $_GET['id'])->find();
		$this->assign('upd',$upd);
		
		if (!empty($_POST['content'])) {
			unlink($upd['content']);
			$dirname = 'Upload/question/';
			$_POST['content'] = '{literal}' . $_POST['content'] . '{/literal}';
			$z = $que->writefile($dirname, $_POST['content']);
			if ($z) {
				$_POST['content'] = $z;
			}
		}
		
		if (!empty($_POST['answer'])) {
			unlink($upd['answer']);
			$dirname = 'Upload/answer/';
			$_POST['answer'] = '{literal}' . $_POST['answer'] . '{/literal}';
			$z = $que->writefile($dirname, $_POST['answer']);
			if ($z) {
				$_POST['answer'] = $z;
			}
		$_POST['add_time'] = strtotime(date('Y-m-d H:i:s'));
		$zz = $que->where('id=' . $_GET['id'])->save($_POST);
		if ($zz) {
			$this->redirect('Tips/tips');
			}
		}

		include ('top.php');
		$this->display();
	}
	/**
	 * 删除试题
	 */
	function del() {
		$del = D('Question');
		$info = $del->where('id=' . $_GET['id'])->find();
		unlink($info['content']);unlink($info['answer']);
		$z = $del->where('id=' . $_GET['id'])->delete();
		if ($z) {
			$this->redirect('Tips/tips');
		}
	}
}